package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.ls.LSInput;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private shoppingCartController shoppingCartController;


    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {

        orderService.submit(orders);


        return R.success("下单成功");
    }

    /**
     * 订单明细分类查询
     *
     * @param page
     * @param pageSize
     * @param number
     * @param endTime
     * @return
     */

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, String beginTime, String endTime) {
        //构建分页构造器
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        //订单号模糊查询
        wrapper.eq(!StringUtils.isEmpty(number), Orders::getNumber, number);
        //时间区间查询
        wrapper.between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);

        Page<Orders> ordersPage1 = orderService.page(ordersPage, wrapper);
        return R.success(ordersPage);

    }

    /**
     * 订单明细状态修改
     *
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> status(@RequestBody Orders orders) {

        orderService.updateById(orders);

        return R.success("状态修改成功");
    }

    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize) {

        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Orders::getOrderTime);
        wrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        orderService.page(ordersPage, wrapper);

        Page<OrdersDto> ordersDtoPage = new Page<>();

        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        List<OrdersDto> collect = ordersPage.getRecords().stream().map(item -> {

            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(OrderDetail::getOrderId, item.getId());
            List<OrderDetail> list = orderDetailService.list(wrapper1);
            ordersDto.setOrderDetails(list);

            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(collect);

        return R.success(ordersDtoPage);
    }

    /**
     * 再来一单
     */
    @PostMapping("/again")
    public R<String> again(HttpServletRequest httpServletRequest, @RequestBody Orders orders) {
        //购物车中如果有数据，将购物车中的数据进行删除
        R<List<ShoppingCart>> nowshoppingCart = shoppingCartController.list();
        if (nowshoppingCart != null) {
            shoppingCartController.clean();
        }
        //订单明细表中的数据
        LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(orders.getId() != null, OrderDetail::getOrderId, orders.getId());
        List<OrderDetail> list = orderDetailService.list(wrapper);
        for (OrderDetail orderDetail : list) {
            //创建ShoppingCart空对象
            ShoppingCart shoppingCart = new ShoppingCart();
            //复制属性
            BeanUtils.copyProperties(orderDetail, shoppingCart);
            //添加至购物车中
            shoppingCartController.add(shoppingCart);
        }
        return R.success("再来一单成功");
    }
}